package com.habitreformer.core.habit;

import java.util.Date;
import java.util.List;

import com.habitreformer.core.task.Task;
import com.habitreformer.core.task.TaskFactory;
import com.habitreformer.core.task.TaskRating;

import env.Env;
import env.Environmental;

public class HabitImpl implements Habit
{
	private int index;
	private String name;
	private Date startDate;
	private List<Task> taskList;
	private HabitStatus status;
	private static Environmental environment;

	public HabitImpl(String name)
	{
		super();
		this.name = name;
		this.index = 0;
		environment = Env.getInstance();
		startDate = environment.getDate();
		taskList = TaskFactory.getInstance().getTaskList();
		status = HabitStatus.PROCEEDING;
	}

	@Override
	public int getCurrentIndex()
	{
		return index;
	}

	@Override
	public String getName()
	{
		return name;
	}

	@Override
	public Date getStartDate()
	{
		return startDate;
	}

	@Override
	public List<Task> getTaskList()
	{
		return taskList;
	}

	@Override
	public void setName(String name)
	{
		this.name = name;
	}

	public HabitStatus getStatus()
	{
		return status;
	}

	public boolean isCompleted()
	{
		return HabitStatus.COMPLETED == status;
	}

	@Override
	public void finishCurrentTask(TaskRating rating)
	{
		if (!isCompleted())
		{
			Task task = taskList.get(index);
			task.finishTask(rating);
			index++;

			if (index >= taskList.size())
			{
				complete();
			}
		}
	}

	void complete()
	{
		// TODO Complete the habit
		status = HabitStatus.COMPLETED;
		// TODO notify
	}
}
